<template>
  <div class="swiper-container" ref="SwiperRef" id="mySwiper">
    <slot>
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="banner in list" :key="banner.id">
          <img :src="banner.imgUrl" />
        </div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>

      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </slot>
  </div>
</template>

<script>
import Swiper from "swiper/swiper-bundle.min.js";
import "swiper/swiper-bundle.min.css";
export default {
  name: "SwiperList",
  props: {
    list: {
      required: true,
    },
    options: {
      type: Object,
      default: () => ({
        autoplay: {
          pauseOnMouseEnter: true,
          disableOnInteraction: false,
        },
        loop: true, // 循环模式选项

        // 如果需要分页器
        pagination: {
          el: ".swiper-pagination",
        },

        // 如果需要前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      }),
    },
  },
  data() {
    return {
      mySwiper: null,
    };
  },
  watch: {
    list: {
      handler(val, ovr) {
        if (val && val.length) {
          this.$nextTick(() => {
            this.initSwiper();
          });
        }
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    initSwiper() {
      this.mySwiper = new Swiper(this.$refs.SwiperRef, this.options);
    },
  },
};
</script>

<style></style>
